﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Business;
using Ecreate.Model;
namespace Ecreate.Web.pages
{
    public partial class customerview : System.Web.UI.Page
    {
        protected string areaname = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string action = this.Request.QueryString["action"];
                this.action.Value = action;
                if ("edit".Equals(action))
                {

                    string cus_id = this.Request.QueryString["id"];
                    CustomerService service = new CustomerService();
                    t_customer model = service.getCustomerByID(cus_id);
                    if (model != null)
                    {
                        this.txt_cus_id.Text = model.Cus_id;

                        this.txt_cus_name.Text = model.Cus_name;
                        this.txt_cus_name.ReadOnly = true;
                        this.txt_cus_address.Text = model.Cus_address;
                        this.txt_cus_address.ReadOnly = true;

                        this.txt_cus_linkman.Text = model.Cus_linkman;
                        this.txt_cus_linkman.ReadOnly = true;


                        this.txt_cus_tel.Text = model.Cus_tel;
                        this.txt_cus_tel.ReadOnly = true;



                        this.cus_id.Value = model.Cus_id;
                        string areacode = model.Areacode;
                        string provincecode = model.Areacode.Substring(0, 2);
                        string citycode = model.Areacode.Substring(0, 4);
                        this.setDropDownList(provincecode, citycode, areacode);

                        return;
                    }
                }
                else if ("delete".Equals(action))
                {

                    string cus_id = this.Request.QueryString["id"];
                    CustomerService service = new CustomerService();
                    t_customer model = service.getCustomerByID(cus_id);
                    if (model != null)
                    {
                        this.txt_cus_name.Text = model.Cus_name;
                        this.txt_cus_address.Text = model.Cus_address;
                        this.txt_cus_linkman.Text = model.Cus_linkman;
                        this.txt_cus_tel.Text = model.Cus_tel;
                        this.cus_id.Value = model.Cus_id;
                    }
                }

                this.setDropDownList("","","");
            }
            else
            {
                string action = (this.Request.Form["action"]);
                if(!String.IsNullOrEmpty(action))
                {

                    if ("edit".Equals(action))
                    {
                        if (this.ddl_province.SelectedIndex == -1)
                        {
                            this.lbl_message.Text = "请选择省份";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        if (this.ddl_city.SelectedIndex == -1)
                        {
                            this.lbl_message.Text = "请选择城市";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        if (this.ddl_country.SelectedIndex == -1)
                        {
                            this.lbl_message.Text = "请选择县";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        if (this.txt_cus_id.Text == "")
                        {
                            this.lbl_message.Text = "请输入用户ID";
                            this.lbl_message.Visible = true;
                            return;
                        }

                        if (this.txt_cus_name.Text == "")
                        {
                            this.lbl_message.Text = "请输入用户名称";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        if (this.txt_cus_address.Text == "")
                        {
                            this.lbl_message.Text = "请输入销售商地址";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        if (this.txt_cus_tel.Text == "")
                        {
                            this.lbl_message.Text = "请输入销售商电话";
                            this.lbl_message.Visible = true;
                            return;
                        }
                        if (this.txt_cus_linkman.Text == "")
                        {
                            this.lbl_message.Text = "请输入联系人";
                            this.lbl_message.Visible = true;
                            return;
                        }






                        CustomerService service = new CustomerService();
                        t_customer cus = new t_customer();
                        cus.Cus_id = this.Request.Form["cus_id"];
                        cus.Cus_name = this.Request.Form["txt_cus_name"];
                        cus.Cus_address = this.Request.Form["txt_cus_address"];
                        cus.Cus_linkman = this.Request.Form["txt_cus_linkman"];
                        cus.Cus_tel = this.Request.Form["txt_cus_tel"];
                        cus.Areacode = this.ddl_country.SelectedValue.ToString();
                        service.Update(cus);

                        Response.Write("<script type=\"text/javascript\" language=\"javascript\">          alert('数据保存成功!')　　; </script>");
                        Response.Write("<script type=\"text/javascript\" language=\"javascript\">          window.parent.frames[\"client\"].location=\"/systemanage/customer/customerlist.aspx\"　　; </script>");


                    }
                }


            }
        }

        public void setDropDownList(string provincecode,string citycode,string countrycode)
        {



            AreaService aservice = new AreaService();

            if (provincecode == "")
            {
                this.ddl_province.DataSource = aservice.getProvinceArea();
            }
            else
            {
                this.ddl_province.DataSource = aservice.getProvinceArea(provincecode);
            }
            this.ddl_province.DataValueField = "areacode";
            this.ddl_province.DataTextField = "areaname";
            this.ddl_province.DataBind();

            if (ddl_province.SelectedValue != null)
            {
                string id = ddl_province.SelectedValue.ToString();
                this.ddl_city.DataSource = aservice.getCityArea(id);
                this.ddl_city.DataValueField = "areacode";
                this.ddl_city.DataTextField = "areaname";
                this.ddl_city.DataBind();
                if (citycode != "")
                {
                    ddl_city.SelectedValue = citycode;
                }


            }
            if (ddl_city.SelectedValue != null)
            {
                string id = ddl_city.SelectedValue.ToString();
                this.ddl_country.DataSource = aservice.getCountryArea(id);
                this.ddl_country.DataValueField = "areacode";
                this.ddl_country.DataTextField = "areaname";
                this.ddl_country.DataBind();
                if (countrycode != "")
                {

                    this.ddl_country.SelectedValue = countrycode;
                }
            }

        }




        protected void ddl_province_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_province.SelectedValue!=null)
            {
                string id = ddl_province.SelectedValue.ToString();
                AreaService aservice = new AreaService();
                this.ddl_city.DataSource = aservice.getCityArea(id);
                this.ddl_city.DataValueField = "areacode";
                this.ddl_city.DataTextField = "areaname";
                this.ddl_city.DataBind();


            }
        }

        protected void ddl_city_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_city.SelectedValue != null)
            {
                string id = ddl_city.SelectedValue.ToString();
                AreaService aservice = new AreaService();
                this.ddl_country.DataSource = aservice.getCountryArea(id);
                this.ddl_country.DataValueField = "areacode";
                this.ddl_country.DataTextField = "areaname";
                this.ddl_country.DataBind();
            }
        }
    }
}
